﻿using ComTools.Attributes;
using ComTools.Helpers;
using ComTools.Reflection;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace ComTools.Extensions
{
    /// <summary>
    /// 实体过滤验证类
    /// </summary>
    public static class AttributeExtend
    {
        /// <summary>
        /// 验证实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tModel"></param>
        public static void Validate<T>(this T tModel, Dictionary<string, object> dic) where T : new()
        {
            Type type = tModel.GetType();
            ValidateType(type, dic);
        }


        private static void ValidateType(Type typeInfo, Dictionary<string, object> dic)
        {
            foreach (var prop in typeInfo.GetProperties())
            {
                if (prop.PropertyType.FullName.Contains("System.") && prop.PropertyType.FullName.IndexOf("System.Collections.Generic.") == -1)
                {
                    if (prop.IsDefined(typeof(AbstractValidateAttribute), true))
                    {
                        object[] attributeArray = prop.GetCustomAttributes(typeof(AbstractValidateAttribute), true);
                        foreach (AbstractValidateAttribute attribute in attributeArray)
                        {
                            //if (string.IsNullOrEmpty(dic[prop.Name].ToString()))
                            if (attribute.Validate(dic[prop.Name]))
                            {
                                object[] objs = prop.GetCustomAttributes(typeof(DescriptionAttribute), true);
                                if (objs.Length > 0)
                                {
                                    throw new Exception(string.Format("{0}不可为空!", ((DescriptionAttribute)objs[0]).Description));
                                }
                                else
                                {
                                    throw new Exception(string.Format("{0}不可为空!", prop.Name));//, prop.GetValue(tModel, null) == null ? "null" : prop.GetValue(tModel, null).ToString()
                                }
                            }
                        }
                    }
                }
                else
                {
                    ///如果是list实体
                    if (prop.PropertyType.FullName.IndexOf("System.Collections.Generic.") > -1)
                    {
                        string fullName = prop.PropertyType.FullName.Substring(prop.PropertyType.FullName.IndexOf("[[") + 2,
                                                        prop.PropertyType.FullName.LastIndexOf("]]") - prop.PropertyType.FullName.IndexOf("[[") - 2);

                        fullName = fullName.Substring(0, fullName.IndexOf(","));

                        var vaules = dic[prop.Name].ObjectToJSON().JSONToObject<List<Dictionary<String, Object>>>();

                        foreach (var item in vaules)
                        {
                            Type requestModel = Assembly.Load(fullName.Substring(0, fullName.IndexOf('.'))).CreateInstance(fullName, false).GetType();
                            //string path = fullName + "," + assemblyName;//命名空间.类型名,程序集
                            //Type o = Type.GetType(path);//加载类型

                            //string path = fullName + "," + fullName.Substring(0, fullName.IndexOf('.'));//命名空间.类型名,程序集
                            //Type o = Type.GetType(path);//加载类型

                            ValidateType(requestModel, item);
                        }
                    }
                    //非list实体
                    else
                    {
                        ValidateType(prop.PropertyType, dic[prop.Name].ObjectToDic());
                    }
                }
            }
        }
    }
}
